﻿package com.cw.utils
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.FrameLabel;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.system.ApplicationDomain;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	import flash.utils.Timer;

	/**
	 * 通用方法集。
	 * @author DanielLu
	 */
	public class CommUtils
	{
		private static var timer : Timer = new Timer ( 20 , 1 );
		{
		timer.addEventListener ( TimerEvent.TIMER_COMPLETE , __onTicker );
		}

		private static function __onTicker ( e : Event ) : void
		{
			timer.reset ();
			timer.stop ();
			//
			for each (var f:Function in methods)
			{
				f ();
			}
			methods = new Dictionary ( true );
			// trace( methods.length );
		}

		private static var methods : Dictionary = new Dictionary ( true );

		/**
		 * 在下一帧中执行函数方法。
		 * 相同的函数将不再添加
		 */
		public static function executeAtNextFrame ( method : Function ) : void
		{
			methods[method] = method;
			if (! timer.running)
			{
				timer.start ();
			}
		}

		/**
		 * parent是否是child的父容器
		 */
		public static function isAncestor ( parent : DisplayObjectContainer , child : DisplayObject ) : Boolean
		{
			if (parent == null || child == null) return false;
			var p : DisplayObjectContainer = child.parent;
			while (p != null)
			{
				if (p == parent)
				{
					return true;
				}
				p = p.parent;
			}
			return false;
		}

		/**
		 * 深拷贝
		 */
		public static function deepClone ( obj : * ) : *
		{
			var copier : ByteArray = new ByteArray ();
			copier.writeObject ( obj );
			copier.position = 0;
			return copier.readObject ();
		}

		public static function createDisplayObjectInstance ( fullClassName : String , applicationDomain : ApplicationDomain = null ) : DisplayObject
		{
			return createInstance ( fullClassName , applicationDomain ) as DisplayObject;
		}

		public static function createInstance ( fullClassName : String , applicationDomain : ApplicationDomain = null ) : *
		{
			var assetClass : Class = getClass ( fullClassName , applicationDomain );
			if (assetClass != null)
			{
				return new assetClass ();
			}
			return null;
		}

		public static function getClass ( fullClassName : String , applicationDomain : ApplicationDomain = null ) : Class
		{
			if (applicationDomain == null)
			{
				applicationDomain = ApplicationDomain.currentDomain;
			}
			var assetClass : Class = applicationDomain.getDefinition ( fullClassName ) as Class;
			return assetClass;
		}

		public static function addFrameScript ( mc : MovieClip , id : Object , listener : Function ) : void
		{
			if (id is Number)
			{
				mc.addFrameScript ( id , listener );
			}
			else if (id is String)
			{
				var lbls : Array = mc.currentLabels;
				for each (var lbl:FrameLabel in lbls)
				{
					if (lbl.name == id)
					{
						mc.addFrameScript ( lbl.frame - 1 , listener );
					}
				}
			}
			else
			{
				//
			}
		}

		/**
		 * 获取随机数
		 */
		public static function getRandom ( min : Number , max : Number ) : Number
		{
			var randomNum : Number = Math.floor ( Math.random () * (max - min + 1) ) + min;
			return randomNum;
		}
	}
}
